package com.object.configuration.utils;

import cn.hutool.extra.qrcode.QrCodeUtil;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.object.configuration.config.PayConfig;
import org.springframework.util.Base64Utils;

/**
 * 支付工具
 */

public class PayUtile {

    private static AlipayClient alipayClient = new DefaultAlipayClient(PayConfig.URL, PayConfig.APP_ID, PayConfig.APP_PRIVATE_KEY, PayConfig.FORMAT, "utf-8", PayConfig.ALIPAY_PUBLIC_KEY, PayConfig.SIGN_TYPE);


    public static AlipayClient getAlipayClient() {
        return alipayClient;
    }

    /**
     * 获取支付二维码
     *
     * @param order_id    订单号
     * @param price       价格
     * @param productName 产品名
     * @param paymentTime 下单最晚时间
     * @return
     */
    public static String getPay(String order_id, Double price, String productName, Integer paymentTime) {
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest(); //创建API对应的request类
        request.setBizContent("{" +
                "\"out_trade_no\":\"" + order_id + "\"," + //商户订单号
                "\"total_amount\":\"" + price + "\"," +
                "\"subject\":\"" + productName + "\"," +
                "\"store_id\":\"NJ_001\"," +
                "\"timeout_express\":\"" + paymentTime + "m\"}"); //订单允许的最晚付款时间
        AlipayTradePrecreateResponse response = null;
        request.setReturnUrl("/user");
        byte[] bytes;
        try {
            response = alipayClient.execute(request);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        //  System.out.println(response.getQrCode());
        //System.out.println(response.getQrCode());
        bytes = QrCodeUtil.generatePng(response.getQrCode(), 150, 150);
        //  String png = QrCodeUtil.generateAsBase64(response.getCode(), new QrConfig().setWidth(150).setHeight(150), "png");
        String encode = Base64Utils.encodeToString(bytes);
        //  httpResponse.getWriter().write(response.getQrCode());
        return encode;
    }


    /**
     * 获取支付状态
     *
     * @param order_id 订单号
     * @return
     */
    public static Double getPaymentSuccess(String order_id) {
        AlipayTradeQueryRequest request1 = new AlipayTradeQueryRequest();//创建API对应的request类
        request1.setBizContent("{" +
                "\"out_trade_no\":\"" + order_id + "\"," +
                "      \"query_options\":[" +
                "        \"trade_settle_info\"" +
                "      ]" +
                "  }");
        AlipayTradeQueryResponse response1 = null;//通过alipayClient调用API，获得对应的response类
        try {
            response1 = alipayClient.execute(request1);
        } catch (AlipayApiException e) {

        }
        System.out.println(response1.getBody());
        //获取支付的状态码 10000 已经预支付  不知道是否成功支付
        int i = Integer.parseInt(response1.getCode());
        //获取支付的金额
        double amount = Double.parseDouble(response1.getBuyerPayAmount());
        if (i == 10000) {
            if (amount > 0) {
                return amount;
            } else {
                return 0.01;
            }
        }
        return null;
    }

}
